package com.service.ljl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.dao.ljl.Deliverdao;
import com.dao.ljl.invoicedao;
import com.dao.ljl.ododao;
import com.dao.ljl.odomybatis;
import com.dao.ljl.orderdao;
import com.entity.Deliver;
import com.entity.Invoice;
import com.entity.Odo;
import com.entity.Order;

@Service
@Transactional(rollbackFor = ServiceException.class)
public class odoservice {
	@Autowired
	private ododao ododao;
	@Autowired
	private orderdao orderdao;
	@Autowired
	private Deliverdao deliverdao;
	@Autowired
	private odomybatis odomybatis;
	
	@Autowired
	private invoicedao invoicedao;

	/**
	 * @param odo
	 *            娣诲姞鍑哄簱鍗�
	 */
	public void add(Odo odo) {

		ododao.save(odo);
	}

	public void addodo(Map map) {
		Long orid = JSONObject.parseObject(map.get("orid").toString(), Long.class);
		Order order = orderdao.findOne(orid);
		Odo odo = JSONObject.parseObject(map.get("odo").toString(), Odo.class);
		List<Deliver> delivers = new ArrayList<>();
		List<Integer> deids = (List<Integer>) map.get("deids");
		for (int i = 0; i < deids.size(); i++) {
			Deliver deliver = deliverdao.findOne(deids.get(i).longValue());
			delivers.add(deliver);
		}
		odo.setOrder(order);
		odo.setDelivers(delivers);
		List<Invoice> invoices = new ArrayList<>();
		Invoice invoice = new Invoice();
		invoice.setOdo(odo);
		invoice.setInState(new BigDecimal(1));
		invoices.add(invoice);
		odo.setInvoices(invoices);
		ododao.save(odo);
	}

	public void update(Map map) {
		Long orid = JSONObject.parseObject(map.get("orid").toString(), Long.class);
		Order order = orderdao.findOne(orid);
		Odo odo = JSONObject.parseObject(map.get("odo").toString(), Odo.class);
		Long inid=JSONObject.parseObject(map.get("inid").toString(),Long.class);
		List<Invoice> invoices=new ArrayList<>();
		invoices.add(invoicedao.findOne(inid));
		odo.setInvoices(invoices);
		odo.setOrder(order);
		
		ododao.save(odo);
	}

	/**
	 * @param id
	 * @param state
	 *            淇敼鍑哄簱鐘舵��
	 */
	public void updatestate(Long id, BigDecimal state) {
		Odo odo = ododao.findOne(id);
		odo.setOdoState(state);
		ododao.save(odo);
	}

	public void delete(Long id) {
		ododao.delete(id);
	}

	public Map findall(int pagesize, int currage) {
		Map<Object, Object> map = new HashMap<>();
		List<Odo> odos = odomybatis.findallodo();
		int start = (currage - 1) * pagesize;
		int finish = currage * pagesize;
		if (odos.size() < finish) {
			finish = odos.size();
		}
		System.out.println(odos);
		for (int i = 0; i < odos.size(); i++) {

			if (odos.get(i).getInvoices().get(0).getInState().compareTo(new BigDecimal(0)) == 0) {
				odos.get(i).setState("未出库");
			} else if (odos.get(i).getInvoices().get(0).getInState().compareTo(new BigDecimal(1)) == 0) {
				odos.get(i).setState("已出库");
			} else if (odos.get(i).getInvoices().get(0).getInState().compareTo(new BigDecimal(2)) == 0) {
				odos.get(i).setState("已发货");
			} else {
				odos.get(i).setState("退货");
			}
		}
		map.put("odos", odos.subList(start, finish));
		map.put("total", odos.size());
		return map;
	}

	public void odo(Long id) {
		Odo odo = ododao.findOne(id);
		odo.getInvoices().get(0).setInState(new BigDecimal(1));
	}

	public void odos(List<Long> ids) {
		for (int i = 0; i < ids.size(); i++) {
			Odo odo = ododao.findOne(ids.get(i));
			odo.getInvoices().get(0).setInState(new BigDecimal(1));
		}
	}

	public void revoke(Long id) {
		Odo odo = ododao.findOne(id);
		odo.getInvoices().get(0).setInState(new BigDecimal(0));
	}

	public void revokes(List<Long> ids) {
		for (int i = 0; i < ids.size(); i++) {
			Odo odo = ododao.findOne(ids.get(i));
			odo.getInvoices().get(0).setInState(new BigDecimal(0));
		}
	}

	public void invoice(Long id) {
		Odo odo = ododao.findOne(id);
		odo.getInvoices().get(0).setInState(new BigDecimal(2));
	}

	public void tuihuo(Long id) {
		Odo odo = ododao.findOne(id);
		odo.getInvoices().get(0).setInState(new BigDecimal(3));
	}

	public void invoices(List<Long> ids) {
		for (int i = 0; i < ids.size(); i++) {
			Odo odo = ododao.findOne(ids.get(i));
			odo.getInvoices().get(0).setInState(new BigDecimal(2));
		}
	}

	public List<Odo> findone(Long id) {
		List<Odo> odos = odomybatis.findonebyid(id);
		for (int i = 0; i < odos.size(); i++) {
			if (odos.get(i).getInvoices().get(0).getInState().compareTo(new BigDecimal(0)) == 0) {
				odos.get(i).setState("未出库");
			} else if (odos.get(i).getInvoices().get(0).getInState().compareTo(new BigDecimal(1)) == 0) {
				odos.get(i).setState("已出库");
			} else if (odos.get(i).getInvoices().get(0).getInState().compareTo(new BigDecimal(2)) == 0) {
				odos.get(i).setState("已发货");
			} else {
				odos.get(i).setState("退货");
			}
		}
		return odos;
	}

}
